﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basic4Net.BusinessRules;
using Basic4Net.BusinessRules.View;
using Basic4Net.Controls;
using Basic4Net.Entities;
using Basic4Net.OAWeb.Base;


namespace Basic4Net.OAWeb.Module_Workload
{
    public partial class HistoryWorkLoad : BasePage
    {

        protected IList<TeachPostEntity> teacherList = null;
        protected IList<TeachWorkNumList> teachWorkList = null;
        protected List<t_UnSetWork> worklist = new List<t_UnSetWork>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                worklist = UnSetWork.UnSetWorkInstance.FindAll(SchoolMark);
                InitDropList();
                Bind(0, aspNetPager.PageSize);
            }
        }


        /// <summary>
        /// 初始化职称下拉框
        /// </summary>
        private void InitDropList()
        {
            List<t_Post> list = Post.PostInstance.FindAll(SchoolMark);

            //添加一个“其他”选项
            //t_Post item = new t_Post();
            //item.PostName = "无职称";
            //item.RID = -1;
            //list.Add(item);
            dropList1.DataSource = list;
            dropList1.DataValueField = "RID";
            dropList1.DataTextField = "PostName";
            dropList1.DataBind();

        }

        protected string GetHeadInfo()
        {
            string str = "";
            worklist = UnSetWork.UnSetWorkInstance.FindAll(SchoolMark);
            if (worklist != null && worklist.Count > 0)
            {
                foreach (t_UnSetWork item in worklist.ToList())
                {
                    str += "<th>";
                    str += item.WorkName;
                    str += "</th>";
                }
            }
            return str;
        }


        protected string GetTdInfo(int teacherId)
        {
            string str = "";

            if (worklist == null)
                worklist = UnSetWork.UnSetWorkInstance.FindAll(SchoolMark);

            foreach (t_UnSetWork item in worklist)
            {
                str += "<td>";
                int workId = item.RID;
                List<TeachWorkNumList> newList = teachWorkList.ToList().FindAll(delegate(TeachWorkNumList t)
                {
                    return t.TeacherID == teacherId &&
                      t.WorkID == workId;
                });
                if (newList.Count > 0)
                {
                    foreach (TeachWorkNumList item2 in newList)
                    {
                        if (item2.WorkID == item.RID)
                        {
                            str += item2.WorkNum;
                        }
                    }
                }
                else
                {
                    str += "";
                }
                str += "</td>";
            }
            return str;
        }
 

        private void Bind(int startAt, int step)
        {
            //string teachName = TeachName.Text;
            string teachCode = TeachCode.Text;
            int postId = dropList1.SelectedItem.Value == null ? -1 : Convert.ToInt32(dropList1.SelectedItem.Value);
            int count = 0;
            int year = 0, month = 0;
            string s = txtYear.Text;
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out year);
            s = txtMonth.Text;
            if (!string.IsNullOrEmpty(s))
                int.TryParse(s, out month);

            //  List<object> newList = new List<object>();
            teachWorkList = USWorkList.USWorkListInstance.GetWorkNumHistoryInfo(startAt, step, teachCode, SchoolMark, year,
                                                                       month,
                                                                       postId, ref count);
            teacherList = PostRelation.PostRelationInstance.GetTeachInfoByPost(startAt, step, postId, teachCode, SchoolMark, ref count);

            TeachCode.Text = "";//清空
            aspNetPager.RecordCount = count;
            Repeater1.DataSource = teacherList;
            Repeater1.DataBind();

        }


        protected void btnItemBind_Click(object sender, EventArgs e)
        {
            Bind((aspNetPager.PageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }

        [System.Web.Services.WebMethod]
        public static int SetTeachWorkNum(int listId, int workId, int teacherId, int workNum)
        {
            if (teacherId <= 0 || workId <= 0 || workNum <= 0)
                return -1;
            t_USWorkList entity = new t_USWorkList();
            entity.SchoolMark = SchoolMark;
            entity.FYear = CurrentYear;
            entity.FMonth = CurrentMonth;
            entity.TeacherID = teacherId;
            entity.WorkID = workId;
            entity.WorkNum = workNum;
            entity.RID = listId;

            bool b = USWorkList.USWorkListInstance.SetTeachWorkNum(entity);
            if (b)
                return 1;
            else
                return -1;
        }


        /// <summary>
        /// 设置教师课时
        /// </summary>
        /// <param name="arrTeacher"></param>
        /// <returns></returns>
        [System.Web.Services.WebMethod]
        public static int UpdateTeachWorkNum(int listId, int workNum)
        {
            if (listId <= 0)
                return -1;
            bool b = USWorkList.USWorkListInstance.UpdateTeachWorkNum(listId, workNum);
            if (b)
                return 1;
            else
                return -1;
        }


        [System.Web.Services.WebMethod]
        public static int AddTeachWorkNum(int teacherId, int workId, int workNum)
        {
            if (teacherId <= 0 || workId <= 0 || workNum <= 0)
                return -1;
            t_USWorkList entity = new t_USWorkList();
            entity.SchoolMark = SchoolMark;
            entity.FYear = CurrentYear;
            entity.FMonth = CurrentMonth;
            entity.TeacherID = teacherId;
            entity.WorkID = workId;
            entity.WorkNum = workNum;

            bool b = USWorkList.USWorkListInstance.AddTeachWorkNum(entity);
            if (b)
                return 1;
            else
                return -1;
        }


        /// <summary>
        /// 职务分类下拉事件
        /// </summary>
        protected void DrPostChanged(object sender, EventArgs e)
        {
            Bind(0, aspNetPager.PageSize);
        }

        /// <summary>
        /// 分页控件事件
        /// </summary>
        protected void AspNetPagerPageChanged(object sender, BNPager.PageChangedEventArgs e)
        {
            TeachCode.Text = "";//清空

            aspNetPager.PageIndex = e.NewPageIndex;
            Bind((e.NewPageIndex - 1) * aspNetPager.PageSize, aspNetPager.PageSize);
        }
    }
}